<?php
/*********************************************************
This file accepts data from 1R_Customer-Buy and inserts
into the customer db tables
*********************************************************/

/*********************************************************
includes	
*********************************************************/
require_once 'config.inc';
require_once '_common.inc';
require_once '_customer.inc';
require_once '_db.inc';
require_once '_rsa.inc';
require_once '_ui.inc';

/*********************************************************
set the correct time zone to eliminate warnings in debug
*********************************************************/
date_default_timezone_set('America/New_York');

$script_tz = date_default_timezone_get();

if (strcmp($script_tz, ini_get('date.timezone'))){
//    echo 'Script timezone differs from ini-set timezone.';
} 

/*********************************************************
get and set the variables from 1R_Customer-Buy
*********************************************************/
$mo_to_copy = $_POST["test"];

/*********************************************************
connect to db
*********************************************************/
$customer_conn = db_connect(CUSTOMER_DB_USER, CUSTOMER_DB_PASSWORD);

/*********************************************************
insert into table mo
*********************************************************/
$sql = "select max(version)
		from mo_detail_customer where uniqueness_string = '" . $mo_to_copy . "'";

$q_results = db_do_query($customer_conn, $sql);

foreach($q_results as $qr) {
	$cur_version = $qr['MAX(VERSION)'];
	$new_version =  $cur_version + 2;
}

$sql = "select * from mo_detail_customer where uniqueness_string = '" . 
		$mo_to_copy . "' and version = " . $cur_version;

$q_results = db_do_query($customer_conn, $sql);

foreach($q_results as $qr) {
	$cur_batch = $qr['BATCH'];
	$cur_val = $qr['VAL'];
	$cur_r = $qr['R'];
	$cur_mo_n = $qr['MO_N'];
	$cur_blinded_mo = $qr['BLINDED_MO'];
	$cur_bank_sig = $qr['BANK_SIG'];
}

$sql = "insert into mo_detail_customer
		(batch, uniqueness_string, version, val, r, mo_n, blinded_mo, bank_sig, spent)
		values (" . $cur_batch . ", '" . $mo_to_copy . "', " . 
		$new_version . ", " . $cur_val . ", '" . $cur_r . "', " .
		$cur_mo_n . ", '" . $cur_blinded_mo . "', '" . $cur_bank_sig . "', 0)";

$stmt = oci_parse($customer_conn, $sql);

oci_execute($stmt);

$return_statment = "Successfully copied Money Order " . $mo_to_copy;

/*********************************************************
close db connection
*********************************************************/
oci_close($customer_conn);

?>

<HTML>

<HEAD>
	<title>Money Orders Created</title>
</HEAD>

<?php
ui_print_header('Customer - Money Order Copied By Customer');
echo $return_statment;
?>

<br></br>

<?php
ui_print_footer(date('Y-m-d H:i:s'));
?>

